package com.jcp.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jcp.entity.FalseNote;
import com.jcp.entity.Users;
import com.jcp.entity.UsersLogin;
import com.jcp.serviceimp.RequestsServiceImp;
import com.jcp.util.LayuiData;
import com.jcp.util.PageData;

/**
 * Servlet implementation class StaffRequestServlet
 */
@WebServlet("/stareq.do")
public class StaffRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    RequestsServiceImp requestsServiceImp=new RequestsServiceImp();   
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StaffRequestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		doQurry(request, response);
		String op="";
		if (request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		if(op.equals("del")) {
			if (request.getParameter("id") != null) {

				String id = request.getParameter("id");

				boolean flag = requestsServiceImp.del(Integer.parseInt(id));

				PrintWriter out = response.getWriter();
				out.print(flag);
				out.close();

			}
			doQurry(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void doQurry(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置预期响应的类型
		response.setContentType("application/json");
		// 创建printwriter
		PrintWriter out = response.getWriter();

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}
		int userId=1;
		if (request.getSession().getAttribute("user")!=null) {

			UsersLogin users=(UsersLogin)request.getSession().getAttribute("user");

			userId=users.getUserId();
		}
		// 下面的语句应该改为userId
		PageData<FalseNote> pd= requestsServiceImp.getFalseNoteByUserId(page, limit, userId);
		LayuiData<FalseNote> layuiData = new LayuiData<>(0, "", pd.getTotalCount(), pd.getData());
		System.out.println("假条状况："+pd.getData());
		String returnvalue = new Gson().toJson(layuiData);
		
		out.print(returnvalue);

		out.close();
	}

}
